<template>
  <div>
  <a-modal v-model="adjustFlag" :title="title" 
        width="1200px" :bodyStyle ="{
          // 'height':'500px',
        }" >
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="status" slot-scope="status">
           <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="checkNum" slot-scope="text,record" >
          <a-input-number v-if="record.auditStatus=='0' && bpmStatus=='1' "  v-model="record.checkNum"  placeholder="请输入盘点数量" style="width:130px" @blur="checkNum(record,text)"  />
          <span v-if="(text || text==0) && bpmStatus!='1'">{{text}}</span> 
        </template>
<!--        迭代16.7：仓库盘点点击明细的时候在字段”盈亏数量“后面加一个字段“盘点说明”输入框，字符200。-->
        <template slot="checkRemark" slot-scope="text,record" >
          <a-textarea v-if="record.auditStatus=='0' && bpmStatus=='1' "  v-model="record.checkRemark" :max-length="200"  placeholder="请输入盘点说明" style="width:250px" @blur="checkRemarkEditHandle(record,text)"  />
          <span v-if="(text || text != null || text != '') && bpmStatus!='1'">{{text}}</span>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
      </a-table>
    </div>
     <template slot="footer">
        <a-button @click.stop="()=>{adjustFlag=false}">关闭</a-button>
      </template>
  </a-modal>
  <!-- table区域-begin -->
    <div v-if="showFlag">
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <template slot="status" slot-scope="status">
           <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="checkNum" slot-scope="text,record" >
          <span >{{text}}</span> 
        </template>
        <template slot="checkRemark" slot-scope="text,record" >
          <span >{{text}}</span>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
      </a-table>
    </div>
    </div>
</template>

<script>

  import { httpAction,postAction, getAction } from '@/api/manage'
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import { mapGetters } from 'vuex';
  import moment from 'moment'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'

  export default {
    name: 'ErpPurchaseWarehouseCheckList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      SelectUserByDep
    },
    data () {
      return {
        bpmStatus:"",
        showFlag:false,
        checkDate:[],
        today:moment().format('YYYY-MM-DD'),
        title:"仓库明细",
        formDisabled:false,
        adjustFlag:false,
        model:{},
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        validatorRules: {
          remark: [
              { required: true, message: '请输入盘点说明!'},
          ],
        },
        description: '仓库盘点管理页面',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'物品存放仓库',
            align:"center",
            dataIndex: 'storehouseId_dictText',
          },
          {
            title:'物料编号',
            align:"center",
            dataIndex: 'materialBillId',
          },
          {
            title:'物料名称',
            align:"center",
            dataIndex: 'materialName_dictText',
          },
          {
            title:'规格型号',
            align:"center",
            dataIndex: 'specification',
          },
          {
            title:'库存数量',
            align:"center",
            dataIndex: 'warehouseNum',
          },
          {
            title:'盘点数量',
            align:"center",
            dataIndex: 'checkNum',
            width:100,
            scopedSlots: {customRender: 'checkNum'},
          },
          {
            title:'盈亏数量',
            align:"center",
            dataIndex: 'profitLossNum',
          },
          //迭代16.7：仓库盘点点击明细的时候在字段”盈亏数量“后面加一个字段“盘点说明”输入框，字符200。
          {
            title:'盘点说明',
            align:"center",
            scopedSlots: {customRender: 'checkRemark'},
            width:300,
            dataIndex: 'checkRemark',
          },
        ],
        url: {
          editCheckNum:"/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/editCheckNum",
          edit:"/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/edit",
          adjust:"/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/adjust",
          stocktakingWareHouse:"/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/stocktakingWareHouse",
          list: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/getBillDetail",
          delete: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/delete",
          deleteBatch: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/deleteBatch",
          exportXlsUrl: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/exportXls",
          importExcelUrl: "api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/importExcel",
          submit: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/submit",
          submitBatch: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/submitBatch",
          revoke: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/revoke",
          revokeBatch: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/revokeBatch",
          counterTrial: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/counterTrial",
          counterTrialBatch: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/counterTrialBatch",
          audit: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/audit",
          auditBatch: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/auditBatch",
          overrule: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/overrule",
          overruleBatch: "/api/erp/purchase/storehouse/erpPurchaseWarehouseCheck/overruleBatch",
        },
        dictOptions:{},
        superFieldList:[],
        disableMixinCreated:true,
      }
    },
    created() {
      this.getSuperFieldList();
      // 初始化字典配置 在自己页面定义
      this.initDictConfig()
      this.initColumns()
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      show(id,bpmStatus){
        this.bpmStatus=bpmStatus;
        this.adjustFlag=true;
        this.queryParam.id=id;
        this.loadData();
        if(bpmStatus=='1'){
          this.columns = [
            {
              title: '序号',
              dataIndex: '',
              key:'rowIndex',
              width:60,
              align:"center",
              customRender:function (t,r,index) {
                return parseInt(index)+1;
              }
            },
            {
              title:'物品存放仓库',
              align:"center",
              dataIndex: 'storehouseId_dictText',
            },
            {
              title:'物料编号',
              align:"center",
              dataIndex: 'materialBillId',
            },
            {
              title:'物料名称',
              align:"center",
              dataIndex: 'materialName_dictText',
            },
            {
              title:'规格型号',
              align:"center",
              dataIndex: 'specification',
            },
            {
              title:'盘点数量',
              align:"center",
              dataIndex: 'checkNum',
              width:100,
              scopedSlots: {customRender: 'checkNum'},
            },
            {
              title:'盘点说明',
              align:"center",
              scopedSlots: {customRender: 'checkRemark'},
              width:300,
              dataIndex: 'checkRemark',
            },
          ]
        } else {
          this.columns = [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'物品存放仓库',
            align:"center",
            dataIndex: 'storehouseId_dictText',
          },
          {
            title:'物料编号',
            align:"center",
            dataIndex: 'materialBillId',
          },
          {
            title:'物料名称',
            align:"center",
            dataIndex: 'materialName_dictText',
          },
          {
            title:'规格型号',
            align:"center",
            dataIndex: 'specification',
          },
          {
            title:'库存数量',
            align:"center",
            dataIndex: 'warehouseNum',
          },
          {
            title:'盘点数量',
            align:"center",
            dataIndex: 'checkNum',
            width:100,
            scopedSlots: {customRender: 'checkNum'},
          },
          {
            title:'盈亏数量',
            align:"center",
            dataIndex: 'profitLossNum',
          },
          //迭代16.7：仓库盘点点击明细的时候在字段”盈亏数量“后面加一个字段“盘点说明”输入框，字符200。
          {
            title:'盘点说明',
            align:"center",
            scopedSlots: {customRender: 'checkRemark'},
            width:300,
            dataIndex: 'checkRemark',
          },
          ]
        }
      },
      showBpm(id,flag){
        this.showFlag=true;
        this.queryParam.id=id;
        this.queryParam.flag=flag;
        this.loadData();
      },
      checkDateChange(date, dateString) {
        this.$set(this.queryParam, 'checkDateStartTime', dateString[0] + ' 00:00:00')
        this.$set(this.queryParam, 'checkDateEndTime', dateString[1] + ' 23:59:59')
      },
      handleSubmitOne: function (record) {
        if(record.checkNum || record.checkNum==0){
          var that = this
          getAction(that.url.submit, {
            id: record.id
          }).then(res => {
            if (res.success) {
              that.$message.success(res.message)
              that.loadData()
            } else {
              that.$message.warning(res.message)
            }
          })
          return ;
        }
        this.$message.warning('请输入盘点数量');
        
      },
      //调整成功
      handleAdjustOk(){
        this.$refs.form.validate(valid => {
          if (valid) {
            this.model.adjustmentType=this.model.adjustmentType=='1'?'2':'1';
            postAction(this.url.adjust,this.model).then(res => {
              if(res.success){
                this.$message.success(res.message);
                this.loadData();
              }else{
                this.$message.warning(res.message);
              }
            })
            this.adjustFlag = false;
          }
        })
      },
      //调整按钮
      handleAdjust(record,flag){
        console.log(flag);
        this.title="调整";
        this.formDisabled=flag;
        this.adjustFlag=true;
        let userinfo=this.userInfo();
        this.$set(this.validatorRules, 'remark', [
            { required: true, message: '请输入盘点说明'},
        ])
        let remark=null;
        let adjustment=userinfo.empId;
        let adjustmentDate=moment().format('YYYY-MM-DD HH:mm:ss');
        if(flag){
          if(this.$refs.form!=undefined){
            this.$refs.form.clearValidate('remark');
          }
          this.title="调整详情";
          remark=record.remark;
          adjustment=record.adjustment;
          adjustmentDate=record.adjustmentDate;
        }
        this.$set(this.model, 'remark', remark);
        this.$set(this.model, 'id', record.id);
        this.$set(this.model, 'adjustment', adjustment);
        this.$set(this.model, 'adjustmentDate',adjustmentDate);
        this.$set(this.model, 'warehouseNum', record.warehouseNum);
        this.$set(this.model, 'checkNum', record.checkNum);
        this.$set(this.model, 'profitLossNum', record.profitLossNum);
        this.$set(this.model, 'adjustmentType', record.adjustmentType);
        this.$set(this.model, 'storehouseId', record.storehouseId);
        this.$set(this.model, 'materialBillId', record.materialBillId);
        this.$set(this.model, 'materialName', record.materialName);
        this.$set(this.model, 'specification', record.specification);
        this.$set(this.model, 'adjustmentWarehouseNum', record.warehouseNum+record.profitLossNum);
        
      },
      ...mapGetters(['userInfo']),
      //盘点
      handleCheck(){
        let userinfo=this.userInfo();
        getAction(this.url.stocktakingWareHouse,{ employeeId: userinfo.empId, userName: userinfo.empName}).then(res => {
          if(res.success){
            this.$message.success(res.message);
            this.loadData();
          }else{
             this.$message.warning(res.message);
          }
        })
      },
      checkNum(record,text){
        if(!(text+"").match(/^[0-9]*[1-9][0-9]*$|0/)){
            this.$message.warning('请输入正确的盘点数量');
            return ;
        }
        getAction(this.url.editCheckNum,{ id: record.id,checkNum:text }).then(res => {
          if(res.success){
            this.$message.success(res.message);
            // this.loadData();
          }
        })
      },
      //迭代16.7：仓库盘点点击明细的时候在字段”盈亏数量“后面加一个字段“盘点说明”输入框，字符200
      checkRemarkEditHandle(record,text){
        httpAction(this.url.edit,{ id: record.id,checkRemark:text },'put').then((res)=>{
          if(res.success){
            this.$message.success(res.message);
            // this.loadData();
          }
        })
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'employeeId',text:'盘点人',dictCode:''})
        fieldList.push({type:'date',value:'checkDate',text:'盘点时间'})
        fieldList.push({type:'string',value:'storehouseId',text:'物品存放仓库',dictCode:''})
        fieldList.push({type:'string',value:'materialBillId',text:'物料编号',dictCode:''})
        fieldList.push({type:'string',value:'materialName',text:'物料名称',dictCode:''})
        fieldList.push({type:'string',value:'specification',text:'规格型号',dictCode:''})
        fieldList.push({type:'int',value:'warehouseNum',text:'库存数量',dictCode:''})
        fieldList.push({type:'int',value:'checkNum',text:'盘点数量',dictCode:''})
        fieldList.push({type:'int',value:'profitLossNum',text:'盈亏数量',dictCode:''})
        fieldList.push({type:'int',value:'auditStatus',text:'审核状态',dictCode:''})
        fieldList.push({type:'string',value:'adjustment',text:'调整人',dictCode:''})
        fieldList.push({type:'date',value:'adjustmentDate',text:'调整日期'})
        fieldList.push({type:'string',value:'adjustmentType',text:'调整状态',dictCode:''})
        fieldList.push({type:'int',value:'adjustmentWarehouseNum',text:'调整后库存数量',dictCode:''})
        fieldList.push({type:'Text',value:'remark',text:'盘点说明',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
